<!DOCTYPE html>
<html lang="en">

    <head>
        <meta charset="UTF-8" />
        <link rel="stylesheet" href="styles/style.css">
        <link rel="stylesheet" href="styles/theme/atom-one-dark.css">
        <title>例子29-ES6/7常用语法介绍</title>
    </head>

    <body>
        <pre>
            <code>
                // 例子29

                let prop1 = 'foo';
                let prop2 = 'bar';

                let o = {
                        [prop1]: 2,
                        [prop2]: true,
                        [prop1 + prop2]: {},
                        ['say' + 'Hello']() {
                                return 'Hello';
                        },
                        'new data': []
                }
                console.log(o[prop1]); // 2, 或o.foo
                console.log(o[prop2]); // true, 或o.bar
                console.log(o[prop1 + prop2]); // {}, 或o.foobar
                console.log(o['say' + 'Hello']()); // Hello，或o.sayHello()
                console.log(o['new data']); // []

                //--------------------------------------------------------------------------

                // 正确
                const foo = 'bar';
                const baz = { [foo]: foo};
                console.log(baz); // {bar: "bar"}

                // 错误
                const foo = 'bar';
                const baz = { [foo] }; // Uncaught SyntaxError: Unexpected token }
                console.log(baz);
            </code>
        </pre>
        <script src="scripts/libs/highlight.pack.js"></script>
        <script src="scripts/29.js"></script>
        <script>
            hljs.initHighlightingOnLoad();
        </script>
    </body>

</html>